﻿
using GCC.Container.Abstracts;

namespace WebResource
{
    internal class LocalFileManager
    {
        public static readonly LocalFileManager Instance = new();
        private LocalFileManager() { }

        /// <summary>
        /// 当前点名称
        /// </summary>
        public string PointName { get; private set; } = "Forgot to init.";
        /// <summary>
        /// 本地文件地址
        /// </summary>
        public string LocalResourceFolder { get; private set; } = AppContext.BaseDirectory;

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="context"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public void Init(AbsGCCContext context, AbsGCCLoggerProvider logger)
        {
            logger.CreateLogger<LocalFileManager>().Information($"Try to init point{context.PointName} resource.");
            PointName = context.PointName!;
            var localFolder = context.GetConfigByPath(new string[] { PointName!, "LocalResourceFolder" });
            if (localFolder is null)
            {
                throw new ArgumentNullException($"Can not find the LocalResourceFolder config from config server. Point is ${PointName}");
            }
            LocalResourceFolder = localFolder;
        }

        /// <summary>
        /// 是否是本地的合法目录
        /// </summary>
        /// <param name="path"></param>
        public bool IsLocalFolderPath(string path)
        {
            if (Directory.Exists(path))
            {
                return false;
            }
            return Directory.Exists(Path.Combine(this.LocalResourceFolder, path));
        }


        /// <summary>
        /// 检查是否是本地文件路径
        /// </summary>
        /// <param name="filePath">文件路径</param>
        /// <returns></returns>
        public bool CheckFilePath(string filePath, out string validPath)
        {
            validPath = "";
            if (File.Exists(filePath))
            {
                return false;
            }
            if (filePath.StartsWith("\\"))
            {
                filePath = filePath[1..];
            }
            validPath = Path.Combine(this.LocalResourceFolder, filePath);
            return File.Exists(validPath);
        }

        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="path">路径</param>
        /// <returns></returns>
        public string[] GetFiles(string path)
        {
            if (path.StartsWith("\\"))
            {
                path = path[1..];
            }
            if (Directory.Exists(Path.Combine(this.LocalResourceFolder, path)))
            {
                return Directory.GetFiles(Path.Combine(this.LocalResourceFolder, path));
            }
            return Array.Empty<string>();

        }
        /// <summary>
        /// 获取文件夹列表
        /// </summary>
        /// <param name="path">路径</param>
        /// <returns></returns>
        public string[] GetFolders(string path)
        {
            if (path.StartsWith("\\"))
            {
                path = path[1..];
            }
            if (Directory.Exists(Path.Combine(this.LocalResourceFolder, path)))
            {
                return Directory.GetDirectories(Path.Combine(this.LocalResourceFolder, path));
            }
            return Array.Empty<string>();
        }

    }
}
